#!/bin/sh

usage() {
  echo "usage $0 [-d] name.recipe"
}

calibre=$HOME/local/calibre

format="epub"

while getopts 'dhf:' opt; do
  case $opt in
    d)
      debug="--debug-pipeline debug"
      ;;
    f)
      format=$OPTARG
      ;;
    h)
      usage
      exit 1
      ;;
    *)
      usage
      exit 1
      ;;
  esac
done

shift $(($OPTIND - 1))

if [ "x$1" == "x" ]; then
  usage
  exit 1
fi

recipe="$1"
output="${recipe%.recipe}.$format"
echo "recipe: $recipe"
echo "output: $output"
$calibre/ebook-convert $recipe $output $debug
